<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>炫彩图片分类</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            text-align: center;
            background: linear-gradient(135deg, #ff9a9e, #fad0c4);
            padding: 50px;
            color: white;
        }
        .container {
            background: rgba(255, 255, 255, 0.2);
            padding: 20px;
            border-radius: 15px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
            width: 50%;
            margin: auto;
        }
        input[type="file"] {
            display: none;
        }
        .upload-btn {
            background: #ff758c;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 25px;
            cursor: pointer;
            font-size: 16px;
            transition: 0.3s;
        }
        .upload-btn:hover {
            background: #ff1e56;
        }
        #result {
            font-size: 20px;
            margin-top: 20px;
        }
        .image-container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 300px;
        }
        #preview {
            max-width: 100%;
            max-height: 300px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>上传图片进行分类</h2>
        <label for="fileInput" class="upload-btn">选择图片</label>
        <input type="file" id="fileInput" accept="image/*">
        <button class="upload-btn" onclick="uploadImage()">上传并分类</button>
        <p id="result"></p>

        <!-- 图片显示区域 -->
        <div class="image-container">
            <img id="preview">
        </div>
    </div>

    <script>
        function uploadImage() {
            let file = document.getElementById("fileInput").files[0];
            if (!file) {
                alert("请选择一张图片！");
                return;
            }

            let formData = new FormData();
            formData.append("file", file);

            fetch("/predict", { method: "POST", body: formData })
                .then(response => response.json())
                .then(data => {
                    document.getElementById("result").innerText = "预测类别: " + data.prediction;
                    let reader = new FileReader();
                    reader.onload = function(e) {
                        let img = document.getElementById("preview");
                        img.src = e.target.result;
                        img.style.display = "block";
                    };
                    reader.readAsDataURL(file);
                })
                .catch(error => console.error("错误:", error));
        }
    </script>
</body>
</html>
